// routes/juhe-inter.js

const Joi = require("joi");
const Config = require("../config");
const _request = require("../utils/request");

const GROUP_NAME = "juhe";
//接口来自聚合数据 https://www.juhe.cn/myData
module.exports = [
    {
        method: "GET",
        path: `/${GROUP_NAME}/getZodiac`,
        handler: async (request, reply) => {
            let result = await _request.get('http://web.juhe.cn:8080/constellation/getAll',{
                'consName': request.query.consName,
                'type': request.query.type,
                'key': Config.jhZodiacCodeKey,
            });
            if (result.data) {
                if (!!result.data.errcode && !!result.data.errmsg) {
                    reply({
                        errorCode: -1,
                        errorMessage: result.data.errmsg
                    });
                } else {
                    reply({
                        errorCode: 0,
                        errorMessage: "success",
                        result: result.data
                    });
                }
            }
        },
        config: {
            tags: ["api", GROUP_NAME],
            description: "获取星座运势信息",
            auth: false, // 约定此接口不参与 JWT 的用户验证，会结合下面的 hapi-auth-jwt 来使用
            validate: {
                query: {
                    consName: Joi.string().required().description('星座名称'),
                    type: Joi.string().required().description('运势类型：today,tomorrow,week,month,year'),
                }
            }
        }
    },
    {
        method: "GET",
        path: `/${GROUP_NAME}/getHistory`,
        handler: async (request, reply) => {
            let result = await _request.get('http://api.juheapi.com/japi/toh',{
                'v': request.query.v,
                'month': request.query.month,
                'day': request.query.day,
                'key': Config.jhHistoryCodeKey,
            });
            if (result.data) {
                if (!!result.data.errcode && !!result.data.errmsg) {
                    reply({
                        errorCode: -1,
                        errorMessage: result.data.errmsg
                    });
                } else {
                    reply({
                        errorCode: 0,
                        errorMessage: "success",
                        result: result.data
                    });
                }
            }
        },
        config: {
            tags: ["api", GROUP_NAME],
            description: "获取历史上的今天信息",
            auth: false, // 约定此接口不参与 JWT 的用户验证，会结合下面的 hapi-auth-jwt 来使用
            validate: {
                query: {
                    v: Joi.string().required().description('版本：1.0（2.0有问题）'),
                    month: Joi.number().required().description('月份：如10'),
                    day: Joi.number().required().description('日期：如1'),
                }
            }
        }
    },
    {
        method: "GET",
        path: `/${GROUP_NAME}/getHistoryDetail`,
        handler: async (request, reply) => {
            let result = await _request.get('http://api.juheapi.com/japi/tohdet',{
                'v': request.query.v,
                'id': request.query.id,
                'key': Config.jhHistoryCodeKey,
            });
            if (result.data) {
                if (!!result.data.errcode && !!result.data.errmsg) {
                    reply({
                        errorCode: -1,
                        errorMessage: result.data.errmsg
                    });
                } else {
                    reply({
                        errorCode: 0,
                        errorMessage: "success",
                        result: result.data
                    });
                }
            }
        },
        config: {
            tags: ["api", GROUP_NAME],
            description: "获取事件详情",
            auth: false, // 约定此接口不参与 JWT 的用户验证，会结合下面的 hapi-auth-jwt 来使用
            validate: {
                query: {
                    v: Joi.string().required().description('版本：1.0（2.0有问题）'),
                    id: Joi.number().required().description('事件id'),
                }
            }
        }
    },
    {
        method: "GET",
        path: `/${GROUP_NAME}/getLotteryList`,
        handler: async (request, reply) => {
            let result = await _request.get('http://apis.juhe.cn/lottery/types',{
                'key': Config.jhLotteryCodeKey,
            });
            if (result.data) {
                if (!!result.data.errcode && !!result.data.errmsg) {
                    reply({
                        errorCode: -1,
                        errorMessage: result.data.errmsg
                    });
                } else {
                    reply({
                        errorCode: 0,
                        errorMessage: "success",
                        result: result.data
                    });
                }
            }
        },
        config: {
            tags: ["api", GROUP_NAME],
            description: "获取彩票列表",
            auth: false, // 约定此接口不参与 JWT 的用户验证，会结合下面的 hapi-auth-jwt 来使用
        }
    },
    {
        method: "GET",
        path: `/${GROUP_NAME}/getLotteryResult`,
        handler: async (request, reply) => {
            let result = await _request.get('http://apis.juhe.cn/lottery/query',{
                'key': Config.jhLotteryCodeKey,
                'lottery_id': request.query.lottery_id,
                'lottery_no': request.query.lottery_no
            });
            if (result.data) {
                if (!!result.data.errcode && !!result.data.errmsg) {
                    reply({
                        errorCode: -1,
                        errorMessage: result.data.errmsg
                    });
                } else {
                    reply({
                        errorCode: 0,
                        errorMessage: "success",
                        result: result.data
                    });
                }
            }
        },
        config: {
            tags: ["api", GROUP_NAME],
            description: "获取某一期开奖结果",
            auth: false, // 约定此接口不参与 JWT 的用户验证，会结合下面的 hapi-auth-jwt 来使用
            validate: {
                query: {
                    lottery_id: Joi.string().required().description('彩票ID'),
                    lottery_no: Joi.string().description('彩票期号，默认最新一期')
                }
            }
        }
    },
    {
        method: "GET",
        path: `/${GROUP_NAME}/getHistoryLotteryResult`,
        handler: async (request, reply) => {
            let result = await _request.get('http://apis.juhe.cn/lottery/history',{
                'key': Config.jhLotteryCodeKey,
                'lottery_id': request.query.lottery_id,
                'page_size': request.query.page_size,
                'page': request.query.page
            });
            if (result.data) {
                if (!!result.data.errcode && !!result.data.errmsg) {
                    reply({
                        errorCode: -1,
                        errorMessage: result.data.errmsg
                    });
                } else {
                    reply({
                        errorCode: 0,
                        errorMessage: "success",
                        result: result.data
                    });
                }
            }
        },
        config: {
            tags: ["api", GROUP_NAME],
            description: "获取历史开奖结果",
            auth: false, // 约定此接口不参与 JWT 的用户验证，会结合下面的 hapi-auth-jwt 来使用
            validate: {
                query: {
                    lottery_id: Joi.string().required().description('彩票ID'),
                    page_size: Joi.string().description('每次返回条数，默认10，最大50'),
                    page: Joi.string().description('当前页数，默认1')
                }
            }
        }
    },
    {
        method: "GET",
        path: `/${GROUP_NAME}/getWinningCalculator`,
        handler: async (request, reply) => {
            let result = await _request.get('http://apis.juhe.cn/lottery/bonus',{
                'key': Config.jhLotteryCodeKey,
                'lottery_id': request.query.lottery_id,
                'lottery_no': request.query.lottery_no,
                'lottery_res': request.query.lottery_res
            });
            if (result.data) {
                if (!!result.data.errcode && !!result.data.errmsg) {
                    reply({
                        errorCode: -1,
                        errorMessage: result.data.errmsg
                    });
                } else {
                    reply({
                        errorCode: 0,
                        errorMessage: "success",
                        result: result.data
                    });
                }
            }
        },
        config: {
            tags: ["api", GROUP_NAME],
            description: "中奖计算器",
            auth: false, // 约定此接口不参与 JWT 的用户验证，会结合下面的 hapi-auth-jwt 来使用
            validate: {
                query: {
                    lottery_id: Joi.string().required().description('彩票ID'),
                    lottery_no: Joi.string().description('开奖期数，默认最新一期'),
                    lottery_res: Joi.string().required().description('购买的号码，号码之间用英文逗号隔开，红色球和蓝色求之间用@连接，例如：01,11,02,09,14,22,25@05,03'),
                }
            }
        }
    },
    {
        method: "GET",
        path: `/${GROUP_NAME}/getPosByPostCodes`,
        handler: async (request, reply) => {
            let result = await _request.get('http://v.juhe.cn/postcode/query',{
                'key': Config.jhPostCodeKey,
                'postcode': request.query.postcode,
                'page': request.query.page,
                'pagesize': request.query.pagesize,
                'dtype': request.query.dtype
            });
            if (result.data) {
                if (!!result.data.errcode && !!result.data.errmsg) {
                    reply({
                        errorCode: -1,
                        errorMessage: result.data.errmsg
                    });
                } else {
                    reply({
                        errorCode: 0,
                        errorMessage: "success",
                        result: result.data
                    });
                }
            }
        },
        config: {
            tags: ["api", GROUP_NAME],
            description: "通过邮编查询对应的地名",
            auth: false, // 约定此接口不参与 JWT 的用户验证，会结合下面的 hapi-auth-jwt 来使用
            validate: {
                query: {
                    postcode: Joi.string().required().description('邮编，如：215001'),
                    page: Joi.string().description('页数，默认1'),
                    pagesize: Joi.string().description('每页返回，默认:20,最大不超过50'),
                }
            }
        }
    },
    {
        method: "GET",
        path: `/${GROUP_NAME}/getProvinceCityList`,
        handler: async (request, reply) => {
            let result = await _request.get('http://v.juhe.cn/postcode/pcd',{
                'key': Config.jhPostCodeKey,
            });
            if (result.data) {
                if (!!result.data.errcode && !!result.data.errmsg) {
                    reply({
                        errorCode: -1,
                        errorMessage: result.data.errmsg
                    });
                } else {
                    reply({
                        errorCode: 0,
                        errorMessage: "success",
                        result: result.data
                    });
                }
            }
        },
        config: {
            tags: ["api", GROUP_NAME],
            description: "返回省份、城市、地区（县）关联的列表",
            auth: false, // 约定此接口不参与 JWT 的用户验证，会结合下面的 hapi-auth-jwt 来使用
        }
    },
    {
        method: "GET",
        path: `/${GROUP_NAME}/getPostCodeByPos`,
        handler: async (request, reply) => {
            let result = await _request.get('http://v.juhe.cn/postcode/search',{
                'key': Config.jhPostCodeKey,
                'pid': request.query.pid,
                'cid': request.query.cid,
                'did': request.query.did,
                'q': request.query.q,
                'page': request.query.page,
                'pagesize': request.query.pagesize
            });
            if (result.data) {
                if (!!result.data.errcode && !!result.data.errmsg) {
                    reply({
                        errorCode: -1,
                        errorMessage: result.data.errmsg
                    });
                } else {
                    reply({
                        errorCode: 0,
                        errorMessage: "success",
                        result: result.data
                    });
                }
            }
        },
        config: {
            tags: ["api", GROUP_NAME],
            description: "通过地名查询邮编",
            auth: false, // 约定此接口不参与 JWT 的用户验证，会结合下面的 hapi-auth-jwt 来使用
            validate: {
                query: {
                    pid: Joi.string().required().description('省份ID'),
                    cid: Joi.string().required().description('城市ID'),
                    did: Joi.string().description('区域ID'),
                    q: Joi.string().description('地名关键字，如:木渎'),
                    page: Joi.string().description('第几页,默认为第1页'),
                    pagesize: Joi.string().description('每页显示条数,(1-50)默认20条'),
                }
            }
        }
    },
    {
        method: "GET",
        path: `/${GROUP_NAME}/getTouTiaoNews`,
        handler: async (request, reply) => {
            let result = await _request.get('http://v.juhe.cn/toutiao/index',{
                'key': Config.jhTopLineCodeKey,
                'type': request.query.type
            });
            if (result.data) {
                if (!!result.data.errcode && !!result.data.errmsg) {
                    reply({
                        errorCode: -1,
                        errorMessage: result.data.errmsg
                    });
                } else {
                    reply({
                        errorCode: 0,
                        errorMessage: "success",
                        result: result.data
                    });
                }
            }
        },
        config: {
            tags: ["api", GROUP_NAME],
            description: "查询新闻",
            auth: false, // 约定此接口不参与 JWT 的用户验证，会结合下面的 hapi-auth-jwt 来使用
            validate: {
                query: {
                    type: Joi.string().description('类型,,top(头条，默认),shehui(社会),guonei(国内),guoji(国际),yule(娱乐),tiyu(体育)junshi(军事),keji(科技),caijing(财经),shishang(时尚)'),
                }
            }
        }
    },
];
